<?php
namespace app\admin\controller;

class Client extends Admin
{
    public function __construct()
    {
        parent::__construct();
        $this->model = 'Client';
        $this->assign("model", $this->model);
        $where['status'] = 0;
        if ($this->uinfo['roleid'] == 1) {
            $where['roleid'] = 6;
        } elseif ($this->uinfo['roleid'] == 4) {
            $where['roleid'] = 6;
            $where['puserid'] = $this->uinfo['userid'];
        } elseif ($this->uinfo['roleid'] == 6) {
            $where['userid'] = $this->uinfo['userid'];
        }
        $deallist = model('AdminUser')->where($where)->order('userid desc')->column('userid, username');
        $this->assign('deallist', $deallist);
    }
    public function index_stat()
    {

        return $this->fetch();
    }
    // public function index()
    // {

    //     return $this->fetch();
    // }
    protected function get_where_map()
    {
        $source = input("param.source/d");
        $skilled = input("param.skilled/d");
        $status = input("param.status/d");
        $kind = input("param.kind/s");
        $dealerid = input("param.dealerid/d");
        $keywords = input("param.keywords/s");
        $whereDatas = [];
        if ($source) {
            $whereDatas['source'] = $source;
        }
        if ($keywords) {
            $whereDatas['id|name|product|phone|home'] = ['like', "%$keywords%"];
        }
        if ($skilled) {
            $whereDatas['skilled'] = $skilled;
        }
        if ($status) {
            $whereDatas['status'] = $status;
        }
        if ($kind) {
            $whereDatas['kind'] = $kind;
        }
        if ($dealerid) {
            $whereDatas['dealerid'] = $dealerid;
        }
        if($this->uinfo['userid'] != 1 && $this->uinfo['roleid'] != 4) {
            $whereDatas['dealerid'] = $this->uinfo['userid'];
        }
        trace($whereDatas);
        return $whereDatas;
    }
    public function client_deliver()
    {
        $id = input('param.id');
        $info = model($this->model)->get_data(['item_id' => $id]);
        $this->assign('info', $info);
        $this->assign('userid', $this->uinfo['userid']);
        return $this->fetch();
    }
    public function deliver()
    {
        $db = model($this->model);
        $postData = input("post.postData");
        parse_str($postData, $dataarr);
        $info = db('Client')->where('id', $dataarr['id'])->find();
        trace($info);
        unset($info['id']);
        $info['userid'] = $dataarr['touserid'];
        $ids = $db->save_data($info);

        if ($ids === false) {
            return json(['status' => 0, 'msg' => $db->getError()]);
        }
        $db->where('id', $dataarr['id'])->setField('transfer', 1);
        return json(['status' => 1, 'msg' => '操作成功']);
    }
}
